package com.hna.face.controller.business;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hna.face.common.result.BaseResult;
import com.hna.face.model.FaceAcls;
import com.hna.face.model.FaceVehicle;
import com.hna.face.model.PageInfo;
import com.hna.face.service.IFaceAclsService;
import com.hna.face.util.App;
import com.hna.face.util.Config;

import sun.misc.BASE64Decoder;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author cdw
 * @since 2018-08-30
 */
@Controller
@RequestMapping("/acls")
public class AclsController {
	@Autowired
	private IFaceAclsService aclsService;
	
    private final static Logger log = LoggerFactory.getLogger(VehicalController.class);

	  @RequestMapping("/add")
	    public String add(HttpServletRequest request,String username) {
		  request.setAttribute("username", username);
	        return "pages/acls/add";
	    }

	  
//	    @RequestMapping("/vehicle/detail")
//	    public ModelAndView details(HttpServletRequest request) {
//	        ModelAndView modelAndView = new ModelAndView("pages/vehicle/detail");
//	        modelAndView.addObject("id", request.getParameter("id"));
//	        return modelAndView;
//	    }
	//    
	//    
	    @RequestMapping("/vehicle/edit")
		public ModelAndView edit(HttpServletRequest request) {
			ModelAndView modelAndView = new ModelAndView("pages/acls/edit");
			modelAndView.addObject("id", request.getParameter("id"));
			return modelAndView;
		}

		@RequestMapping(value ="/vehicle/aclslist" , method = RequestMethod.POST)
		  @ResponseBody
		  public Map<String, Object> list(HttpServletRequest request,FaceAcls acls,PageInfo pageInfo) {
			Map<String, Object> res = new HashMap<>();
			String username = request.getParameter("username");
			System.out.println(username);
			  try {
		          Wrapper<FaceAcls> wrapper=new EntityWrapper<>();
		          wrapper.eq("username",username);
		          Page<FaceAcls> page = new Page<>(pageInfo.getPage(),pageInfo.getRows());
		          Page<FaceAcls> aclsList = aclsService.selectPage(page, wrapper);
		          res.put("rows", aclsList.getRecords());
		          res.put("total", aclsList.getTotal());
		          res.put("success", "true");
		          return res;
		      } catch (Exception e) {
		          res.put("success", "false");
		          return res;
		      }
		  }
	  
	  @RequestMapping("/vehicle/power")
	  public String psower(HttpServletRequest request,String username) {
		  request.setAttribute("username", username);
	      return "pages/vehicle/power";
	  }

	  
	    @RequestMapping(value = { "/del" }, method = RequestMethod.POST)
	    public @ResponseBody
	    BaseResult del(HttpServletRequest request,String ids) {
	        try {

	        	if(StringUtils.isEmpty(ids)){
	        		return BaseResult.success();
	        	}
	        	aclsService.deleteBatchIds(Arrays.asList(ids.split(",")));
	             return BaseResult.success();
	        } catch (Exception e) {
	        	log.error("发生异常了",e);
	            return BaseResult.fail(e.getMessage());
	        }
	    }
	    
	   
	    
	    @RequestMapping(value = { "/save" }, method = RequestMethod.POST)
	    public @ResponseBody
	    BaseResult save(HttpServletRequest request,String username,FaceAcls acls) {
	        try {
	        	acls.setUsername(username);
	        	acls.setRw(acls.getRw());
	        	acls.setTopic(acls.getTopic());
	        	aclsService.insert(acls);
	        	return BaseResult.success();
	        } catch (Exception e) {
	        	log.error("发生异常了",e);
	            return BaseResult.fail(e.getMessage());
	        }
	    }
	    
	    
	    @RequestMapping(value = { "/rest/vehicle/update" }, method = RequestMethod.POST)
	    public @ResponseBody
	    BaseResult update(HttpServletRequest request, FaceAcls acls,String meterNo) {
	        Map<String, Object> res = new HashMap<>();
	        try {
//	        	vehicle.setMeterNo(meterNo);
	        	aclsService.updateById(acls);
	        	
//	        	MjMeter meter = new MjMeter();
//	        	meter.setvehicleId(vehicle.getId());
//	        	meter.setInstallTime(vehicle.getRegisteredTime());
//	        	meter.setMeterId(meterId);
//				meterService.insert(meter );
	            
	        	return BaseResult.success();
	        } catch (Exception e) {
	        	log.error("发生异常了",e);
	            return BaseResult.fail(e.getMessage());
	        }
	    }

	  
}
